<?php
namespace app\admin\controller;

use \think\Controller as Controller;
use \think\Cache as Cache;
use \think\Validate as Validate;
use \think\Config as Config;

class Booktype extends Base{

    /**
     * 查看所有书本类型
     * 
     * @return html
     * @date Wed Jun 27 17:34:04 2018
     */
    public function index(){
        $page = $this->req->get('page');
        $model = new \app\admin\model\BookType;
        $num = Config::get('paginate.list_rows');
        $data = [];
        if(isset($page)){
            $data = $model->limit($page,$num)->select();
        }else{
            $data = $model->select();
            
        }
        $list = $model->paginate($num);        
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 填写书本类型页面
     * 
     * @return html
     * @date Wed Jun 27 17:34:42 2018
     */
    public function add(){
        $model = new \app\admin\model\BookType;
        $data = $model->select();
        if($data){
            $this->assign('data',$data);
            return $this->fetch();
            
        } else {
            return $this->error('无数据');
        }
    }

    /**
     * 添加图书类型
     * 
     * @return error/success
     * @date Wed Jun 27 22:39:39 2018
     */
    public function insert(){
        $post = $this->req->post();
        $rule = [
            'name' => 'require',
        ];
        $validate = new Validate($rule);

        // $result = $validate->check($post);
        if($validate->check($post)){
            $model = new \app\admin\model\BookType;
            $data = [
                'bt_name' => $post['name'],
                'bt_sn' => $post['sn'],
                'bt_fid' => $post['fid'],
                'bt_sort' => $post['sort']
            ];
            if($model->save($data)){
                echo $this->alert(1,'插入成功');
            } else {
                echo $this->alert(0,'插入失败');
            }
            
        } else{
            echo $this->alert(0,'没有输入名称1');
        }
        return;
    }

    /**
     * 编辑页面
     * 
     * @return json
     * @date Thu Jun 28 20:31:01 2018
     */
    public function edit(){
        $id = $this->req->get();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($id)){
            $model = new \app\admin\model\BookType;
            $data = $model->select();
            $it = [];
            $mount = [];
            foreach($data as $v){
                if($v['bt_id'] == $id['id']){
                    $it = $v;
                }
                $mount[$v['bt_id']] = $v;
                
            }
            foreach($mount as $key=>$m){
                if($key == $it['bt_fid']){
                    $m['selected'] = 'selected';
                }else{
                    $m['selected'] = '';
                }
            }
            $this->assign('it',$it);
            $this->assign('mount',$mount);
            exit($this->fetch());
        }else{
            exit($this->fetch('public/error',['msg'=>'id不正确']));
        }
    }

    /**
     * 修改类型数据
     * 
     * @return 
     * @date Fri Jun 29 10:14:54 2018
     */
    public function update(){
        $post = $this->req->post();
        $rule = [
            'name' => 'require',
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\BookType;
            $data = [
                'bt_name' => $post['name'],
                'bt_sn' => $post['sn'],
                'bt_fid' => $post['fid'],
                'bt_sort' => $post['sort']
            ];
            if($model->where(['bt_id'=>$post['id']])->update($data)){
                echo $this->alert(1,'修改成功');
            } else {
                echo $this->alert(0,'修改失败');
            }
            
        } else{
            echo $this->alert(0,'没有输入名称1');
        }
        return;
    }

    /**
     * 删除一个类型
     * 
     * @return 
     * @date Wed Jul 18 09:57:47 2018
     */
    public function remove(){
        $model = new \app\admin\model\BookType;
        // $result = $model->remove();
        $result = 0;
        if($result){
            echo $this->right(['msg'=>'删除成功']);
        } else {
            echo $this->wrong(['msg'=>'删除失败,删除这个类型以前先删除下面的子类']);
        }
    }
}

